﻿Public Partial Class Maintenance_Wharf
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "MAINTENANCE WHARF "
            Dim boooking As String = Request.QueryString("booking")
            If (boooking & "" = "") Then
                ViewState("Booking_no") = ""
                Dim dt As DataTable = clsGet.Get_WharfAll("L")
                If (dt.Rows.Count > 0) Then
                    With ddlWharf
                        .Items.Clear()
                        .ClearSelection()
                        .DataTextField = "Wharf"
                        .DataValueField = "BookingWharf"
                        .DataSource = dt
                        .DataBind()
                        .Items.Insert(0, "<--Select Wharf-->")
                        .Items(0).Value = "0"
                    End With
                Else
                    With ddlWharf
                        .Items.Clear()
                        .Items.Insert(0, "<--Select Wharf-->")
                        .Items(0).Value = "0"
                    End With
                End If
                If (dt.Rows.Count > 0) Then
                    With ddlWharfSearch
                        .Items.Clear()
                        .ClearSelection()
                        .DataTextField = "Wharf"
                        .DataValueField = "BookingWharf"
                        .DataSource = dt
                        .DataBind()
                        .Items.Insert(0, "<--Select Wharf-->")
                        .Items(0).Value = "0"
                    End With
                Else
                    With ddlWharfSearch
                        .Items.Clear()
                        .Items.Insert(0, "<--Select Wharf-->")
                        .Items(0).Value = "0"
                    End With
                End If
                LoadMaintenance("", "0")
            Else
                ViewState("Booking_no") = boooking
                Dim dt As DataTable = clsGet.Get_BookingWharf(boooking)
                If (dt.Rows.Count > 0) Then
                    If Not (radioPort.Items.FindByValue(dt.Rows(0).Item("PORT") & "") Is Nothing) Then
                        radioPort.SelectedValue = dt.Rows(0).Item("PORT") & ""
                        Dim dw As DataTable = clsGet.Get_WharfAll(radioPort.SelectedValue)
                        If (dt.Rows.Count > 0) Then
                            With ddlWharf
                                .Items.Clear()
                                .ClearSelection()
                                .DataTextField = "Wharf"
                                .DataValueField = "BookingWharf"
                                .DataSource = dw
                                .DataBind()
                                .Items.Insert(0, "<--Select Wharf-->")
                                .Items(0).Value = "0"
                            End With
                        Else
                            With ddlWharf
                                .Items.Clear()
                                .Items.Insert(0, "<--Select Wharf-->")
                                .Items(0).Value = "0"
                            End With
                        End If
                        If Not (ddlWharf.Items.FindByValue(dt.Rows(0).Item("BookingWharf") & "") Is Nothing) Then
                            ddlWharf.SelectedValue = dt.Rows(0).Item("BookingWharf") & ""
                        End If
                    End If
                    txtBerthDate.Text = dt.Rows(0).Item("Date_From")
                    txtBerthTime.Text = dt.Rows(0).Item("TimeStart")
                    txtDepartDate.Text = dt.Rows(0).Item("Date_to")
                    txtDepartTime.Text = dt.Rows(0).Item("TimeEnd")
                    txtRemark.Text = dt.Rows(0).Item("Remark")

                    btnConfirm.Visible = True
                    PanelHome.Visible = False
                    panelMaintenance.Visible = True
                End If

            End If


        End If
    End Sub

    Private Sub LoadMaintenance(ByVal PORT As String, ByVal Wharf As String)
        ViewState("Maintenance") = clsGet.Get_MaintenanceWharf(PORT, Wharf)
        gridMaintenance.DataSource = ViewState("Maintenance")
        gridMaintenance.DataBind()
        PanelHome.Visible = True
        panelMaintenance.Visible = False
    End Sub

    Public Sub Paginate(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim CurrentIndex As Integer = gridMaintenance.PageIndex
        Select Case e.CommandArgument.ToString().ToLower()
            Case "first"
                gridMaintenance.PageIndex = 0
            Case "prev"
                If (gridMaintenance.PageIndex <> 0) Then
                    gridMaintenance.PageIndex = CurrentIndex - 1
                End If
            Case "next"
                If (gridMaintenance.PageIndex <> gridMaintenance.PageCount) Then
                    gridMaintenance.PageIndex = CurrentIndex + 1
                End If
            Case "last"
                gridMaintenance.PageIndex = gridMaintenance.PageCount
        End Select
        gridMaintenance.DataSource = ViewState("Maintenance")
        gridMaintenance.DataBind()
    End Sub

    Protected Sub ddlPage_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim Page As GridViewRow = gridMaintenance.BottomPagerRow
        Dim ddlPage As DropDownList = CType(Page.Cells(0).FindControl("ddlPage"), DropDownList)
        gridMaintenance.PageIndex = ddlPage.SelectedIndex
        gridMaintenance.DataSource = ViewState("Maintenance")
        gridMaintenance.DataBind()
    End Sub

    Private Sub btnMainTenance_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMainTenance.Click
        PanelHome.Visible = False
        panelMaintenance.Visible = True
        ClearText()
    End Sub

    Private Sub btnCLose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCLose.Click
        If (btnConfirm.Visible) Then
            Response.Redirect("../Form/frm_ShipSchedule.aspx?P4=O", True)
        Else
            LoadMaintenance("", "0")
        End If
    End Sub

    Private Sub ClearText()
        radioPort.ClearSelection()
        radioPort.SelectedValue = "L"
        Dim dt As DataTable = clsGet.Get_WharfAll(radioPort.SelectedValue)
        If (dt.Rows.Count > 0) Then
            With ddlWharf
                .Items.Clear()
                .ClearSelection()
                .DataTextField = "Wharf"
                .DataValueField = "BookingWharf"
                .DataSource = dt
                .DataBind()
                .Items.Insert(0, "<--Select Wharf-->")
                .Items(0).Value = "0"
            End With
        Else
            With ddlWharf
                .Items.Clear()
                .Items.Insert(0, "<--Select Wharf-->")
                .Items(0).Value = "0"
            End With
        End If
        ddlWharf.ClearSelection()
        ViewState("Booking_no") = ""
        txtBerthDate.Text = String.Empty
        txtBerthTime.Text = String.Empty
        txtDepartDate.Text = String.Empty
        txtDepartTime.Text = String.Empty
    End Sub

    Private Sub radioPort_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radioPort.SelectedIndexChanged
        Dim dt As DataTable = clsGet.Get_WharfAll(radioPort.SelectedValue)
        If (dt.Rows.Count > 0) Then
            With ddlWharf
                .Items.Clear()
                .ClearSelection()
                .DataTextField = "Wharf"
                .DataValueField = "BookingWharf"
                .DataSource = dt
                .DataBind()
                .Items.Insert(0, "<--Select Wharf-->")
                .Items(0).Value = "0"
            End With
        Else
            With ddlWharf
                .Items.Clear()
                .Items.Insert(0, "<--Select Wharf-->")
                .Items(0).Value = "0"
            End With
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (ddlWharf.SelectedValue = "0") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ WHARF');", True)
            Exit Sub
        End If
        If (txtBerthDate.Text.Trim() = "") Or (txtBerthTime.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ BERTH DATE/TIME');", True)
            Exit Sub
        End If
        If (txtDepartDate.Text.Trim() = "") Or (txtDepartTime.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ DEPART DATE/TIME');", True)
            Exit Sub
        End If
        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Dim User_Create As String = CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("User_name") & " " & DateTime.Now.ToString("yyyyMMdd:HHmmss")
        Application.Lock()
        conn.Open()
        Dim Booking As String = ""
        If (ViewState("Booking_no") & "" = "") Then
            Booking = "M" & CInt(clsDB.GeneratorID(conn, "Maintenance_Wharf"))
        Else
            Booking = ViewState("Booking_no")
        End If

        tran = clsDB.BeginTran(conn)
        clsFc.DeleteBookingWharfMaintenance(Booking, conn, tran)
        If (clsFc.InsertBooking_Wharf(Booking, ddlWharf.SelectedValue, txtBerthDate.Text, txtDepartDate.Text, txtBerthTime.Text, txtDepartTime.Text, txtRemark.Text, User_Create, CDate(txtBerthDate.Text).ToString("dd/MM/yyyy"), CDate(txtDepartDate.Text).ToString("dd/MM/yyyy"), "N", "", "", conn, tran) < 1) Then
            condition = False
        End If
        If (ViewState("Booking_no") & "" = "") Then
            If (clsFc.InsertMaintenance_Wharf(Booking.Substring(1, Booking.Length - 1), Date.Now.ToString("dd MMM yyyy HH:mm:ss"), conn, tran) < 1) Then
                condition = False
            End If
        End If
        If (condition) Then
            clsDB.CommitTran(tran)

            If Not (btnConfirm.Visible) Then
                ClearText()
                LoadMaintenance("", "0")
            End If
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('บันทึกข้อมูลเรียบร้อย');", True)
        Else
            clsDB.RollBackTran(tran)
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('เกิดความผิดพลาดในการบันทึก');", True)
        End If
        Application.UnLock()
        conn.Dispose()
    End Sub

    Private Sub gridMaintenance_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridMaintenance.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If (gridMaintenance.PageIndex <> 0) Then
            Index = Index Mod (gridMaintenance.PageIndex * 10)
        End If
        If e.CommandName = "cmdedit" Then
            ViewState("Booking_no") = gridMaintenance.DataKeys(Index).Values("Booking_no")
            If Not (radioPort.Items.FindByValue(gridMaintenance.DataKeys(Index).Values("PORT") & "") Is Nothing) Then
                radioPort.SelectedValue = gridMaintenance.DataKeys(Index).Values("PORT") & ""
                Dim dt As DataTable = clsGet.Get_WharfAll(radioPort.SelectedValue)
                If (dt.Rows.Count > 0) Then
                    With ddlWharf
                        .Items.Clear()
                        .ClearSelection()
                        .DataTextField = "Wharf"
                        .DataValueField = "BookingWharf"
                        .DataSource = dt
                        .DataBind()
                        .Items.Insert(0, "<--Select Wharf-->")
                        .Items(0).Value = "0"
                    End With
                Else
                    With ddlWharf
                        .Items.Clear()
                        .Items.Insert(0, "<--Select Wharf-->")
                        .Items(0).Value = "0"
                    End With
                End If
                If Not (ddlWharf.Items.FindByValue(gridMaintenance.DataKeys(Index).Values("BookingWharf") & "") Is Nothing) Then
                    ddlWharf.SelectedValue = gridMaintenance.DataKeys(Index).Values("BookingWharf") & ""
                End If
            End If
            txtBerthDate.Text = gridMaintenance.DataKeys(Index).Values("Date_From")
            txtBerthTime.Text = gridMaintenance.DataKeys(Index).Values("TimeStart")
            txtDepartDate.Text = gridMaintenance.DataKeys(Index).Values("Date_to")
            txtDepartTime.Text = gridMaintenance.DataKeys(Index).Values("TimeEnd")
            txtRemark.Text = gridMaintenance.DataKeys(Index).Values("Remark")

            PanelHome.Visible = False
            panelMaintenance.Visible = True
        ElseIf e.CommandName = "cmddelete" Then

            Dim Booking_no As String = gridMaintenance.DataKeys(Index).Values("Booking_no")
            Dim conn As New System.Data.SqlClient.SqlConnection(connString)
            Dim clsDB As New ClassDatabase
            Dim clsFc As New ClassFunction
            Dim condition As Boolean = True
            Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
            Application.Lock()
            conn.Open()
            tran = clsDB.BeginTran(conn)
            If (clsFc.DeleteBookingWharf(Booking_no, conn, tran) < 1) Then
                condition = False
            End If
            If (condition) Then
                clsDB.CommitTran(tran)
                LoadMaintenance("", "0")
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ลบข้อมูลเรียบร้อย!!');", True)
            Else
                clsDB.RollBackTran(tran)
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('เกิดความผิดพลาดในการลบข้อมูล !!');", True)
            End If
            conn.Close()
            Application.UnLock()
            conn.Dispose()
        End If
    End Sub

    Private Sub gridMaintenance_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridMaintenance.RowDataBound
        If (e.Row.RowType = DataControlRowType.Pager) Then
            Dim Page As GridViewRow = gridMaintenance.BottomPagerRow
            Dim ddlPage As DropDownList = CType(e.Row.FindControl("ddlPage"), DropDownList)
            Dim lblPageCount As Label = CType(e.Row.FindControl("lbPageCount"), Label)
            If Not (ddlPage Is Nothing) Then
                For i As Integer = 0 To gridMaintenance.PageCount - 1
                    Dim intPageNumber As Integer = i + 1
                    Dim IsItem As New ListItem(intPageNumber.ToString())
                    If (i = gridMaintenance.PageIndex) Then
                        IsItem.Selected = True
                    End If
                    ddlPage.Items.Add(IsItem)
                Next
            End If
            If Not (lblPageCount Is Nothing) Then
                lblPageCount.Text = gridMaintenance.PageCount.ToString()
            End If
        End If
    End Sub

    Private Sub gridMaintenance_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gridMaintenance.RowEditing

    End Sub

    Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        LoadMaintenance(radioPortSearch.SelectedValue, ddlWharfSearch.SelectedValue)
    End Sub

    Private Sub radioPortSearch_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radioPortSearch.SelectedIndexChanged
        Dim dt As DataTable = clsGet.Get_WharfAll(radioPortSearch.SelectedValue)
        If (dt.Rows.Count > 0) Then
            With ddlWharfSearch
                .Items.Clear()
                .ClearSelection()
                .DataTextField = "Wharf"
                .DataValueField = "BookingWharf"
                .DataSource = dt
                .DataBind()
                .Items.Insert(0, "<--Select Wharf-->")
                .Items(0).Value = "0"
            End With
        Else
            With ddlWharfSearch
                .Items.Clear()
                .Items.Insert(0, "<--Select Wharf-->")
                .Items(0).Value = "0"
            End With
        End If
    End Sub

    Private Sub btnConfirm_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConfirm.Click
        If (ddlWharf.SelectedValue = "0") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ WHARF');", True)
            Exit Sub
        End If
        If (txtBerthDate.Text.Trim() = "") Or (txtBerthTime.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ BERTH DATE/TIME');", True)
            Exit Sub
        End If
        If (txtDepartDate.Text.Trim() = "") Or (txtDepartTime.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ DEPART DATE/TIME');", True)
            Exit Sub
        End If
        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Dim User_Create As String = CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("User_name") & " " & DateTime.Now.ToString("yyyyMMdd:HHmmss")
        Application.Lock()
        conn.Open()
        Dim Booking As String = ""
        If (ViewState("Booking_no") & "" = "") Then
            Booking = "M" & CInt(clsDB.GeneratorID(conn, "Booking"))
        Else
            Booking = ViewState("Booking_no")
        End If

        tran = clsDB.BeginTran(conn)
        clsFc.DeleteBookingWharfMaintenance(Booking, conn, tran)
        If (clsFc.InsertBooking_Wharf(Booking, ddlWharf.SelectedValue, txtBerthDate.Text, txtDepartDate.Text, txtBerthTime.Text, txtDepartTime.Text, txtRemark.Text, User_Create, CDate(txtBerthDate.Text).ToString("dd/MM/yyyy"), CDate(txtDepartDate.Text).ToString("dd/MM/yyyy"), "Y", "", "", conn, tran) < 1) Then
            condition = False
        End If

        If (condition) Then
            clsDB.CommitTran(tran)
            If Not (btnConfirm.Visible) Then
                LoadMaintenance("", "0")
                ClearText()
            End If
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('บันทึกข้อมูลเรียบร้อย');", True)
        Else
            clsDB.RollBackTran(tran)
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('เกิดความผิดพลาดในการบันทึก');", True)
        End If
        Application.UnLock()
        conn.Dispose()
    End Sub


    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub
End Class